package kr.hkit.wordnote.fragment;

import java.util.ArrayList;

import org.apache.http.impl.EnglishReasonPhraseCatalog;

import com.example.wordnote2.R;

import kr.hkit.wordnote.activity.FlipperActivity;
import kr.hkit.wordnote.fragment.WordInputFragment.OnWordChangedListener;
import kr.hkit.wordnote.fragment.WordListFragment.OnEditButtonClickedListener;
import android.R.interpolator;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class WordStudyFragment extends Fragment {
	
	public static WordStudyFragment newInstance(ArrayList<String> engList, 
			ArrayList<String> korList, String wordcount, String delaytime, boolean reset) {
		WordStudyFragment frag = new WordStudyFragment();
		Bundle args = new Bundle();
		args.putSerializable("engList", engList);
		args.putSerializable("korList", korList);
		args.putString("wordcount", wordcount);
		args.putString("delaytime", delaytime);
		args.putBoolean("reset", reset);
		frag.setArguments(args);

		return frag;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		LinearLayout linear = (LinearLayout) inflater.inflate(R.layout.wordstudyframent, container, false);
		
		return linear;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		ArrayList<String> engList = (ArrayList<String>) getArguments().get("engList");
		ArrayList<String> korList = (ArrayList<String>) getArguments().get("korList");
		int wordCount = Integer.parseInt((String)getArguments().get("wordcount"));
		String delayTime = (String)getArguments().get("delaytime");
		boolean reset = (boolean)getArguments().get("reset");
		//chapter의 갯수를 가져온다. 
		int count = getChapterCount(engList, wordCount);
		//갯수만큼 arryList에 'chapter+i' 문자열을 담는다.(리스트 항목의 문자열로 사용)
		ArrayList<String> chapterList = new ArrayList<>();
		for(int i=1; i<=count; i++){
			chapterList.add("chapter"+i);
		}
		
		MyListAdapter myAdapter = new MyListAdapter(getActivity(), R.layout.chapterlist_item, chapterList,
				engList, korList, wordCount, delayTime, reset);
		ListView myList = (ListView)getActivity().findViewById(R.id.sutdylist);
		myList.setAdapter(myAdapter);
	}
	
	//chapter의 갯수를 구하는 메서드 
	public int getChapterCount(ArrayList<String> engList, int wordCount){
		int count = engList.size()/wordCount;
		int nmg = engList.size() - (count*wordCount);
		if(nmg != 0){
			count++;
		}
		return count;
	}
	
}

//내가 정의한 어댑터
class MyListAdapter extends BaseAdapter{
	Context maincon;
	LayoutInflater inflater;
	ArrayList<String> chapterList;
	ArrayList<String> engList;
	ArrayList<String> korList;
	int layout;
	int wordCount;
	String delayTime;
	boolean reset;
	
	public MyListAdapter(Context context, int alayout, ArrayList<String> chList, 
			ArrayList<String> arEng, ArrayList<String> arKor, int wordcount, String delaytime, boolean res){
		maincon = context;
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		chapterList = chList;
		engList = arEng;
		korList = arKor;
		layout = alayout;
		wordCount = wordcount;
		delayTime = delaytime;
		reset = res;
	}
	@Override
	public int getCount() {
		return chapterList.size();
	}

	@Override
	public Object getItem(int position) {
		return chapterList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(layout, parent, false);
		}
		
		final ArrayList<String> engChapterList = new ArrayList<>();
		final ArrayList<String> korChapterList = new ArrayList<>();
		
		//chapter당 시작 단어가 저장된 index를 저장하는 변수
		int chapterBeginNum = position * wordCount;
		int count = 0;
		//각 chapter에 해당하는 단어를 설정 개수씩 chapterList에 담는다.
		for(int i = chapterBeginNum; i<engList.size(); i++){
			engChapterList.add(engList.get(i));
			korChapterList.add(korList.get(i));
			count++;
			if(count >= wordCount){
				count = 0;
				break;
			}
		}
		
		TextView tv = (TextView)convertView.findViewById(R.id.chaptertext);
		tv.setText(chapterList.get(position));
		tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(maincon, FlipperActivity.class);
				intent.putStringArrayListExtra("engChapterList", engChapterList);
				intent.putStringArrayListExtra("korChapterList", korChapterList);
				intent.putExtra("delayTime", delayTime);
				maincon.startActivity(intent);
			}
		});
		
		CheckBox checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
		SharedPreferences pref = maincon.getSharedPreferences("checkBox", 0);
		pref = maincon.getSharedPreferences("checkBox", 0);
		
		//챕터 개수가 변경되면 학습완료 체크 표시가 리셋된다.
		if(reset){
			SharedPreferences.Editor edit = pref.edit();
			edit.putBoolean(position+"", false);
			edit.commit();
		}
		checkbox.setChecked(pref.getBoolean(position+"", false));
		
		
		checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SharedPreferences pref = maincon.getSharedPreferences("checkBox", 0);
				pref = maincon.getSharedPreferences("checkBox", 0);
				SharedPreferences.Editor edit = pref.edit();
				edit.putBoolean(position+"", isChecked);
				edit.commit();
			}
		});
		
		
		return convertView;
	}
}




